#include "test_common.h"

/* This is a simple test of the main functionalities of eix
 * The applications forkes and the child connects to the server the parent
 * creates. You'll see your CPU at 100% but dont worry that's normal
 * because we are not using any main loop handler
 */

int main(void)
{
	Eix_Server *srv = NULL;
	Eix_Transport *t;
	pid_t pid;

	test_common_init();
	/* fork ourselves */
	pid = fork();
	if (pid >= 0)
	{
		/* we are the child */
		if (!pid)
		{
			sleep(1);
			srv = test_common_connect();
			while (!test_end)
			{
				test_common_server_process(srv);
				/* send a question */
				if (test_connected)
				{
					Test_Message_Question q;
					Test_Message_Answer a;

					q.s = "Who are you?";
					eix_server_message_send(srv, TEST_MESSAGE_QUESTION, &q, &a, NULL, NULL);
#if 0
					printf("TC01: recevied %s\n", a.s);
#endif
					test_end = EINA_TRUE;
				}
			}
			printf("TC01: Quitting\n");
			eix_server_del(srv);
		}
		else
		{
			int status;

			/* create the server in one process */
			srv = test_common_serve();
			/* keep looping until we have received a message */
			while (!test_end)
			{
				test_common_server_process(srv);
			}
			do
			{
				if (waitpid(pid, &status, 0) < 0)
				{
					if (errno != ECHILD)
						perror("wrong");
					break;
				}
			} while (!WIFEXITED(status));
			printf("TS01: Quitting\n");
		}
	}
	else
	{
		printf("fork failed\n");
		return -1;
	}

	test_common_shutdown();
	return 0;
}
